package com.first.xiinn.action.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.first.core.util.UUID;
import com.first.core.web.Constants;
import com.first.core.web.action.BaseAction;
import com.first.xiinn.model.BookManager;
import com.first.xiinn.model.Comment;
import com.first.xiinn.model.User;
import com.first.xiinn.service.BookService;
import com.first.xiinn.service.CommentService;
import com.opensymphony.xwork2.ActionContext;

public class CommentWebAction extends BaseAction {
	@Resource
	private BookService bookService;
	@Resource
	private CommentService commentService;
	private Comment comment;
	
	public Comment getComment() {
		return comment;
	}

	public void setComment(Comment comment) {
		this.comment = comment;
	}
	private String bookid = null;
	
	public String getBookid() {
		return bookid;
	}

	public void setBookid(String bookid) {
		this.bookid = bookid;
	}
	public String ifLogin(){
		ActionContext ac = ActionContext.getContext();
		@SuppressWarnings("unchecked")
		Map<String, User> session = ac.getSession();
		User user  = session.get(Constants.USER_SESSION_KEY);
		String success ="false";
		if(user!=null){
			success="true";
		}
		JSONObject json = new JSONObject();
		json.put("success",success);
		HttpServletResponse response =  ServletActionContext.getResponse();
		try {
			response.setHeader("Pragma","No-cache");
			response.setDateHeader("Expires",0);
			response.setHeader("Cache-Control","no-cache"); 
			response.setCharacterEncoding("UTF-8");
			PrintWriter write = response.getWriter();
			write.write(json.toString());
			write.flush();
			write.close();
		} catch (IOException e) {
			e.getMessage();
		}
		return "success";
	}
	
	public String list() {
		System.out.println("111111111111111111");
		return "success";
	}
	
	public String write(){
		BookManager book = this.bookService.get(this.bookid);
		setActionContextValue("book", book);
		ActionContext ac = ActionContext.getContext();
		@SuppressWarnings("unchecked")
		Map<String, User> session = ac.getSession();
		User loginWebUser = null;
		if(session!=null){
			loginWebUser =  (User)session.get(Constants.USER_SESSION_KEY);
		}
		setActionContextValue("loginWebUser", loginWebUser);
		return "write";
	}
	/**
	 * 保存
	 */
	public String save(){
		String msg="";
		String success="flase";
		try {
				comment.setId(UUID.getUUID());
				this.commentService.addComment(comment);
				msg="评论发表成功";
				success="true";
		} catch (Exception e) {
			e.printStackTrace();
			msg="系统错误";
		}finally{
			JSONObject json = new JSONObject();
			json.put("success",success);
			json.put("msg",msg);
			HttpServletResponse response =  ServletActionContext.getResponse();
			try {
				response.setHeader("Pragma","No-cache");
				response.setDateHeader("Expires",0);
				response.setHeader("Cache-Control","no-cache"); 
				response.setCharacterEncoding("UTF-8");
				PrintWriter write = response.getWriter();
				write.write(json.toString());
				write.flush();
				write.close();
			} catch (IOException e) {
				e.getMessage();
			}
		}
		return "success";
	}
}
